// pages/fdetail_mann/fdetail_mann.js
const get_from = require('../../module/service.js').get_from;
const post_to = require('../../module/service.js').post_to;

Page({

  /**
   * 页面的初始数据
   */
  data: {
    // shop_id: 0,
    //食品信息
    food_id: 0,
    name: '',
    categories: [],
    description: '',
    discount: 0,
    img_url: '',
    on_sale: 0,
    price: 0,
    cate_arr: [],
    data_bak: {},
    is_cate_modi: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    let food_id = JSON.parse(options.food_id);
    // let cate_arr = JSON.parse(decodeURIComponent(options.cate_arr));
    let cate_arr = [];
    if (food_id) {
      this.data.food_id = food_id;
      //获取商店的食物
      get_from('food/getFoodByFoodId', {
        food_id: food_id
      }).then((res) => {
        this.setData({
          name: res.data.name,
          categories: JSON.parse(res.data.categories),
          description: res.data.description,
          discount: res.data.discount,
          img_url: res.data.img_url,
          on_sale: res.data.on_sale,
          price: res.data.price
        }, () => {
          //备份数据
          this.data.data_bak = {
            name: this.data.name,
            categories: [...this.data.categories],
            description: this.data.description,
            discount: this.data.discount,
            img_url: this.data.img_url,
            on_sale: this.data.on_sale,
            price: this.data.price
          };

          // console.log(this.data.data_bak.categories);
        });
      });
    }

    //还需要先把数据库foods中的categories全部改成分类才行
    get_from('food/getCategoriesByFoodId', {
      food_id: food_id
    }).then((res) => {
      console.log(res.data);
      cate_arr = res.data;
    });

    setTimeout(() => {
      let tmp = [];
      if (cate_arr) {
        for (let i = 0; i < cate_arr.length; i++) {
          tmp.push({
            cate: cate_arr[i],
            checked: false
          });
        }
        // console.log('tmp: ', tmp);
        for (let i = 0; i < cate_arr.length; i++) {
          //设置checked
          if (tmp[i].cate === this.data.categories[i]) {
            // console.log(tmp[tmp_num-1]);
            tmp[i].checked = true;
          }
        }
        // this.data.cate_arr = tmp;
        this.setData({
          cate_arr: tmp
        })
        console.log(this.data.cate_arr);
      }
    }, 500);
  },

  modi_name(e) {
    if (e.currentTarget.dataset.name === 'reset') {
      //重置
      this.setData({
        name: this.data.data_bak.name
      });
      // this.data.name = e.currentTarget.dataset.cont;
      return;
    }
    wx.showModal({
      title: '修改菜名',
      // content: '这是一个模态弹窗',
      editable: true,
      success: (res) => {
        if (res.confirm) {
          console.log(e.currentTarget.dataset.name);
          console.log(res.content);
          this.setData({
            name: res.content
          });
          // console.log(this.data.data_bak);
        }
      }
    });
  },

  modi_categories(e) {
    //重置
    if (e.currentTarget.dataset.name === 'reset') {
      this.data.categories = this.data.data_bak.categories;
      console.log(this.data.data_bak.categories);
      let cate_arr = this.data.cate_arr;
      for (let i = 0; i < cate_arr.length; i++) {
        cate_arr[i].checked = false;
      }
      // console.log('past cate_arr: ', cate_arr);
      // console.log('categories: ', this.data.categories);
      let len = this.data.categories.length;
      for (let i = 0; i < cate_arr.length; i++) {
        for (let j = 0; j < len; j++) {
          if (cate_arr[i].cate === this.data.categories[j]) {
            cate_arr[i].checked = true;
          }
        }
      }
      // console.log('now cate_arr: ', cate_arr);

      this.setData({
        categories: this.data.data_bak.categories,
        cate_arr: cate_arr
      });
      // this.data.name = e.currentTarget.dataset.cont;
      return;
    }

    this.setData({
      is_cate_modi: true
    });
  },

  checkboxChange(e) {
    // console.log(e.detail.value);
    this.setData({
      categories: e.detail.value
    });
  },

  cate_ensure() {
    let cate_arr = this.data.cate_arr;
    for (let i = 0; i < cate_arr.length; i++) {
      cate_arr[i].checked = false;
    }
    let len = this.data.categories.length;
    for (let i = 0; i < len; i++) {
      for (let j = 0; j < cate_arr.length; j++) {
        if (cate_arr[j].cate === this.data.categories[i]) {
          cate_arr[j].checked = true;
        }
      }
    }
    //设置值
    this.setData({
      is_cate_modi: false,
      cate_arr: cate_arr
    });
    post_to('shop/modicateByFoodId', {
      food_id: this.data.food_id,
      categories: this.data.categories
    }).then((res)=>{
      if (res.data.msg === 'success')
      {
        wx.showToast({
          title: '修改成功',
          icon: 'success'
        });
      }
    });
  },

  cate_can() {
    this.setData({
      is_cate_modi: false
    });
  },

  modi_description(e) {
    if (e.currentTarget.dataset.name === 'reset') {
      //重置
      this.setData({
        description: this.data.data_bak.description
      });
      // this.data.name = e.currentTarget.dataset.cont;
      return;
    }
    wx.showModal({
      title: '修改描述',
      // content: '这是一个模态弹窗',
      editable: true,
      success: (res) => {
        if (res.confirm) {
          console.log(e.currentTarget.dataset.name);
          console.log(res.content);
          this.setData({
            description: res.content
          });
        }
      }
    });
  },

  modi_discount(e) {
    if (e.currentTarget.dataset.name === 'reset') {
      //重置
      this.setData({
        discount: this.data.data_bak.discount
      });
      // this.data.name = e.currentTarget.dataset.cont;
      return;
    }
    wx.showModal({
      title: '修改折扣',
      // content: '这是一个模态弹窗',
      editable: true,
      success: (res) => {
        if (res.confirm) {
          console.log(e.currentTarget.dataset.name);
          console.log(res.content);
          //判断是否是数字
          if (res.content == '' || res.content == undefined) {
            wx.showToast({
              title: '不能为空！',
            });
          } else if (!(/^[0-9]+(.[0-9]{0,2})?$/.test(res.content))) { //验证有0-2位小数的正实数
            wx.showToast({
              title: '只能输入数字！',
              icon: 'error',
              duration: 700
            });
            return;
          }
          //设置值
          this.setData({
            discount: res.content
          });
          // this.data.name = res.content;
        }
      }
    });
  },

  modi_img_url(e) {
    if (e.currentTarget.dataset.name === 'reset') {
      //重置
      this.setData({
        img_url: this.data.data_bak.img_url
      });
      // this.data.name = e.currentTarget.dataset.cont;
      return;
    }
    wx.showModal({
      title: '修改图片url',
      // content: '这是一个模态弹窗',
      editable: true,
      success: (res) => {
        if (res.confirm) {
          console.log(e.currentTarget.dataset.name);
          console.log(res.content);
          this.setData({
            img_url: res.content
          });
          // this.data.name = res.content;
        }
      }
    });
  },

  modi_on_sale() {
    this.setData({
      on_sale: !this.data.on_sale
    });
    console.log(this.data.on_sale);
  },

  modi_price(e) {
    if (e.currentTarget.dataset.name === 'reset') {
      //重置
      this.setData({
        price: this.data.data_bak.price
      });
      // this.data.name = e.currentTarget.dataset.cont;
      return;
    }
    wx.showModal({
      title: '修改价格',
      // content: '这是一个模态弹窗',
      editable: true,
      success: (res) => {
        if (res.confirm) {
          console.log(e.currentTarget.dataset.name);
          console.log(res.content);
          //判断是否是数字
          if (res.content == '' || res.content == undefined) {
            wx.showToast({
              title: '不能为空！',
            });
          } else if (!(/^[0-9]+(.[0-9]{0,2})?$/.test(res.content))) { //验证有0-2位小数的正实数
            wx.showToast({
              title: '只能输入数字！',
              icon: 'error',
              duration: 700
            });
            return;
          }

          this.setData({
            price: res.content
          });
          // this.data.name = res.content;
        }
      }
    });
  },

  ensure() {
    //提交数据到后端
    post_to('shop/modiFoodByFoodId', {
      food_id: this.data.food_id,
      name: this.data.name,
      categories: this.data.categories,
      description: this.data.description,
      discount: this.data.discount,
      img_url: this.data.img_url,
      on_sale: this.data.on_sale,
      price: this.data.price
    }).then((res) => {
      //修改成功响应
      wx.showToast({
        title: '修改成功',
        icon: 'success'
      });
    });

  },

  reset_all() {
    this.setData({
      name: this.data.data_bak.name,
      categories: this.data.data_bak.categories,
      description: this.data.data_bak.description,
      discount: this.data.data_bak.discount,
      img_url: this.data.data_bak.img_url,
      on_sale: this.data.data_bak.on_sale,
      price: this.data.data_bak.price
    });
  },
})